﻿using System;
using System.Collections.Generic;
using System.Text;
using UrlRouter.Configuration;

namespace UrlRouter.Web
{
    /// <summary>
    /// 规则实体
    /// </summary>
    public abstract class RewriteRule
    {

        private RedirectOption redirect = RedirectOption.None; //默认
        /// <summary>
        /// 规则转向属性
        /// </summary>
        public RedirectOption Redirect
        {
            get { return redirect; }
            set { redirect = value; }
        }

        private RewriteOption rewrite = RewriteOption.Application; //默认
        /// <summary>
        /// 规则的重写属性
        /// </summary>
        public RewriteOption Rewrite
        {
            get { return rewrite; }
            set { rewrite = value; }

        }
        private string name;
        /// <summary>
        /// 规则唯一名
        /// </summary>
        public string Name
        {
            get { return name; }
            internal set { name = value; }
        }

        private RedirectModeOption redirectMode = RedirectModeOption.Temporary;
        /// <summary>
        /// 规则的转向模式
        /// </summary>
        public RedirectModeOption RedirectMode
        {
            get { return redirectMode; }
            set { redirectMode = value; }
        }
        private RewriteUrlParameterOption rewriteUrlParameter = RewriteUrlParameterOption.ExcludeFromClientQueryString;
        /// <summary>
        /// 规则的URL参数处理
        /// </summary>
        public RewriteUrlParameterOption RewriteUrlParameter
        {
            get { return rewriteUrlParameter; }
            set { rewriteUrlParameter = value; }
        }

        private bool ignoreCase = false;
        /// <summary>
        /// 规则的忽略大小写 默认为false
        /// </summary>
        public bool IgnoreCase
        {
            get { return ignoreCase; }
            set { ignoreCase = value; }
        }
        /// <summary>
        /// 是否能够匹配重写规则
        /// </summary>
        /// <param name="requestUrl"></param>
        /// <returns></returns>
        public abstract bool IsRewrite(string requestUrl);
        /// <summary>
        /// 正则替换实际url地址
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public abstract string RewriteUrl(string url);

        /// <summary>
        /// 规则实体的初始化调用
        /// </summary>
        /// <param name="rewriteSettings"></param>
        public virtual void Initialize(RewriteSettings rewriteSettings)
        {
            if (rewriteSettings == null)
                throw new ArgumentNullException("rewriteSettings");
            this.redirect = rewriteSettings.Redirect;
            this.rewrite = rewriteSettings.Rewrite;
            this.redirectMode = rewriteSettings.RedirectMode;
            this.rewriteUrlParameter = rewriteSettings.RewriteUrlParameter;
            this.ignoreCase = rewriteSettings.IgnoreCase;
            this.Name = rewriteSettings.Name;
        }
    }
}
